package shiro;

import com.ql.dao.user.RoleMapper;
import com.ql.dao.yk.DoctorMapper;
import com.ql.pojo.user.Role;
import com.ql.pojo.yk.Doctor;
import com.ql.pojo.yk.DoctorExample;
import com.ql.service.doctor.DoctorService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.aspectj.apache.bcel.classfile.ConstantValue;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


public class MyRealm extends AuthorizingRealm {
    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private RoleMapper roleMapper;
    /**
     * 授权:
     *
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //在上边的doGetAuthenticationInfo认证通过填充到SimpleAuthenticationInfo中身份类型
        Doctor doctor = (Doctor) principalCollection.getPrimaryPrincipal();
        if(null != doctor){               //两个if根据判断赋予登录用户权限
            Integer jobtitle = doctor.getJobtitle();
            Role role = roleMapper.selectByPrimaryKey(jobtitle);
            info.addRole(role.getRname());
        }

        return info;
    }

    /*
     * 用户验证
     *
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // 1. 把AuthenticationToken 转换为UsernamePasswordToken
        UsernamePasswordToken up = (UsernamePasswordToken) token;
        /*获取输入的用户名及密码*/
        String username = up.getUsername();
        String password = new String((char[]) up.getPassword());

        /*调用mapper层方法返回用户信息*/
        DoctorExample doctorExample = new DoctorExample();
        DoctorExample.Criteria criteria = doctorExample.createCriteria();
        criteria.andDocloginEqualTo(username);
        List<Doctor> doctors = doctorMapper.selectByExample(doctorExample);
        Doctor doctor=null;
        if(null != doctors && doctors.size() > 0){
            doctor = doctors.get(0);
        }
        /*如果用户信息为Null,则抛出用户不存在异常*/
        if(doctor == null){
            throw new UnknownAccountException("用户名为空-----------------");
        }
        /*如果用户状态为2,则抛出账户注销异常*/
        if(doctor.getState().equals(2)){
            throw new LockedAccountException("账号被注销-----------------------");
        }
        // 可以是username，也可以是数据表对应的用户的实体类对象
        String credentials = doctor.getDocpassword(); // credentials：密码
        String realmName = getName();
        AuthenticationInfo info = null;/*new SimpleAuthenticationInfo(principal, credentials, realmName);*/

        ByteSource credentialsSalt = ByteSource.Util.bytes(username);//这里的参数要给个唯一的;

        info = new SimpleAuthenticationInfo(doctor, credentials, credentialsSalt, realmName);

        return info;
    }



}